using System;
using MediaPortal.GUI.Library;
using System.Text.RegularExpressions;
using System.Net;
using System.Text;
using MediaPortal.Player;
using System.Collections.Generic;
//using MediaPortal.Utils.Services;
using MediaPortal.GUI.View ;
using MediaPortal.Dialogs;
using System.Xml;
using System.Xml.XPath;
using System.ComponentModel;
using System.Threading;



namespace OnlineVideos
{
    public class DailyMotionUtil:SiteUtilBase 
    {
       public override string getSiteId()
		{
			return "6";
		}
               
        protected override String getUrl(String fsId)
        {
            String lsUrl = "";
        	Log.Info("DMotion getUrl with "+fsId);
        	String lsHtml = getHTMLData(fsId);
        	Match loMatch = Regex.Match(lsHtml,"addVariable\\(\"video\",\\s\"([^\"]*)");
        	if(loMatch.Success){
                String lsTemp = loMatch.Groups[1].Value;
                lsTemp = System.Web.HttpUtility.UrlDecode(lsTemp);
                loMatch = Regex.Match(lsTemp, "([^@]*)@@spark");
                if(loMatch.Success){
                    lsUrl = loMatch.Groups[1].Value; 
        		    lsUrl = "http://www.dailymotion.com"+lsUrl+"&txe=.flv";
                    return lsUrl;
                }
        	}
        		Log.Info("Dailymotion video not found. Site could have changed layout.");
        	    return "";
            
        }
        public override List<GUIOnlineVideos.VideoInfo> getVideoList(string fsUrl)
		{
			List<RssItem> loRssItemList = getRssDataItems(fsUrl);
			List<GUIOnlineVideos.VideoInfo> loVideoList = new List<GUIOnlineVideos.VideoInfo>();
			GUIOnlineVideos.VideoInfo video;
			foreach(RssItem rssItem in loRssItemList){
				video = new GUIOnlineVideos.VideoInfo();
				video.Description = rssItem.description;
				video.ImageUrl = rssItem.mediaThumbnail;
				video.Title = rssItem.title;
				video.Length = rssItem.contentList[0].duration;
				video.VideoUrl = rssItem.guid;
				loVideoList.Add(video);
			}
			return loVideoList;
		}
        public List<GUIOnlineVideos.VideoInfo> getRssData(String fsUrl)
        {

            XmlDocument doc = new XmlDocument();
            
            doc.Load(XmlReader.Create(fsUrl));
            XmlNamespaceManager expr = new XmlNamespaceManager(doc.NameTable);
            expr.AddNamespace("media", "http://search.yahoo.com/mrss");

            //Create the root XmlNode and generate an XmlNodeList from it.
            XmlNode root = doc.SelectSingleNode("//rss/channel/item", expr);
            XmlNodeList nodeList;
            nodeList = root.SelectNodes("//rss/channel/item");

            XmlAttributeCollection ac;
            List<GUIOnlineVideos.VideoInfo> loRssItems = new List<GUIOnlineVideos.VideoInfo>();
            //RssItem loRssItem;
            //GUIListItem loListItem;
            GUIOnlineVideos.VideoInfo loVideoInfo;
            foreach (XmlNode chileNode in nodeList)
            {
                //loRssItem = new RssItem();                
                loVideoInfo = new GUIOnlineVideos.VideoInfo();
                XmlNode node = chileNode.SelectSingleNode("title");
                //loRssItem.title = node.InnerText;
                loVideoInfo.Title = node.InnerText;

                //node = chileNode.SelectSingleNode("link");
                //loRssItem.link = node.InnerText;
                

                node = chileNode.SelectSingleNode("description");
                //loRssItem.description = node.InnerText;
                loVideoInfo.Description = node.InnerText;

                node = chileNode.SelectSingleNode("media:thumbnail", expr);
                if (node != null)
                {
                    ac = node.Attributes;
                    //loRssItem.imageUrl = ac["url"].InnerText;
                    loVideoInfo.ImageUrl = ac["url"].InnerText;
                }


                node = chileNode.SelectSingleNode("guid", expr);
                if (node != null)
                {                    
                    //loRssItem.videoUrl = ac["url"].InnerText;
                    loVideoInfo.VideoUrl = node.InnerText;

                }

                //Log.Write(loRssItem.ToString());
                //loListItem = new GUIListItem(loRssItem.title);
                //loListItem.Path = loRssItem.videoUrl;
                loRssItems.Add(loVideoInfo);

                
            }
            return loRssItems;
        }       
    }
}
